package com.sright.bcard.enterprise.biz.manager.impl;

import com.sright.bcard.common.biz.manager.ContentCheckManager;
import com.sright.bcard.common.service.share.dto.page.PageDTO;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.enums.SourceTypeEnum;
import com.sright.bcard.common.service.share.exception.base.BusinessErrorException;
import com.sright.bcard.common.service.share.exception.code.BusinessCode;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.enterprise.biz.manager.AddressManager;
import com.sright.bcard.enterprise.dal.dao.AddressDAO;
import com.sright.bcard.enterprise.dal.entity.AddressDO;
import com.sright.bcard.common.service.share.dto.enterprise.AddressDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class AddressManagerImpl implements AddressManager {

    @Autowired
    private AddressDAO addressDAO;

    @Autowired
    private ContentCheckManager contentCheckManager;

    @Override
    public PageDTO<AddressDTO> addressList(PageParamRequest pageParamRequest) {
        // 查询总的条数
        Integer count = addressDAO.count(pageParamRequest);
        PageDTO<AddressDTO> pageDTO = new PageDTO<>();
        // 分页查询数据
        List<AddressDO> addressDOS = addressDAO.pageList(pageParamRequest);
        if(CollectionUtils.isNotEmpty(addressDOS)) {
            // DTO 转化为 DO
            List<AddressDTO> AddressDTOS = new ArrayList<>();
            DataConvertUtils.convertDOList2DTOList(addressDOS, AddressDTOS, AddressDTO.class);
            pageDTO.setData(AddressDTOS);
            pageDTO.setCount(count);
            return pageDTO;
        }
        return new PageDTO<>();
    }

    @Override
    public Long addAddress(AddressDTO addressDTO) {
        // DTO转化为DO
        if(null == addressDTO) {
            return 0L;
        }
        //新增敏感词审核
        if (StringUtils.isNotEmpty(addressDTO.getAddressZh())) {
            List<String> contentList = new LinkedList<>();
            contentList.add(addressDTO.getAddressZh());
            String checkResult = contentCheckManager.textCheck(contentList,addressDTO.getEnterpriseId(), SourceTypeEnum.ENTERPRISE_BACKEND.getValue());
            if (checkResult.equals("block")) {
                throw new BusinessErrorException(BusinessCode.CONTAIN_SENSITIVE_WORDS);
            }
        }
        AddressDO addressDO = DataConvertUtils.convertDTO2DO(addressDTO, new AddressDO());
        return addressDAO.insertSelective(addressDO);
    }

    @Override
    public Boolean updateAddress(AddressDTO addressDTO) {
        if(null == addressDTO) {
            return false;
        }
        if (StringUtils.isNotEmpty(addressDTO.getAddressZh())) {
            List<String> contentList = new LinkedList<>();
            contentList.add(addressDTO.getAddressZh());
            String checkResult = contentCheckManager.textCheck(contentList,addressDTO.getEnterpriseId(), SourceTypeEnum.ENTERPRISE_BACKEND.getValue());
            if (checkResult.equals("block")) {
                throw new BusinessErrorException(BusinessCode.CONTAIN_SENSITIVE_WORDS);
            }
        }
        AddressDO addressDO = new AddressDO();
        DataConvertUtils.convertDTO2DO(addressDTO, addressDO);
        return addressDAO.updateSelective(addressDO);
    }

    @Override
    public Boolean deleteAddress(List<Long> ids) {
        final Integer[] count = {0};
        if(CollectionUtils.isNotEmpty(ids)) {
            ids.stream().forEach(id -> {
                Integer i = addressDAO.deleteAddress(id);
                if(i == 1) {
                    count[0]++;
                }
            });
        }
        return count[0] > 0;
    }

    @Override
    public List<AddressDO> getList(Long enterpriseId) {
        List<Long> list = Arrays.asList(enterpriseId);
        return addressDAO.selectByEnterpriseIdList(list);
    }
}
